﻿namespace BEGDC.CC.Web.UserControls.OCV
{
    using System;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;

    using BEGDC.CC.BL.CCFlow.OCV;
    using BEGDC.CC.Entity;
    using BEGDC.CC.BL.Workflow;
    using BEGDC.CC.BL.Common;
    using BEGDC.CC.BL.Common.Query;
    using BEGDC.Utility.Castle;
    using BEGDC.CC.ExcelTool;
    using BEGDC.CC.BL.UserManagement;

    /// <summary>
    ///		Summary description for Assignment.
    /// </summary>
    public class Assignment : BaseOCV
    {
        protected System.Web.UI.WebControls.Button Btn_Report;
        protected System.Web.UI.WebControls.Label Label1;
        protected System.Web.UI.WebControls.Button btnSubmit;
        protected System.Web.UI.WebControls.TextBox tbOperationFlag;
        protected System.Web.UI.WebControls.TextBox tbOperationContent;
        protected System.Web.UI.WebControls.TextBox tbComment;
        protected System.Web.UI.WebControls.Button btnCheck;
        protected System.Web.UI.WebControls.Button btnReject;
        protected System.Web.UI.WebControls.Button btnReply;
        protected System.Web.UI.WebControls.Button btnSave;
        protected System.Web.UI.WebControls.Button btnQuery;
        protected System.Web.UI.WebControls.Button btnClose;
        //protected BEGDC.CC.Web.UserControls.CV.CVPrintCtrl cvPrintCtrl;
        protected OCVCtrl uclOCV;

        private bool IsQuery
        {
            get
            {
                string state = this.Request.QueryString["State"];
                if (state == null) return false;
                return state == SysDicItem.ProjectStatusFlag.QUERY;
            }
        }
        protected string Step
        {
            get
            {
                IQuery queryService = BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.CC.BL.Common.ServiceConst.QueryService] as IQuery;
                FlowInstance flowInstance = queryService.QueryFlowInstance(this.AppID);
                if (flowInstance == null)
                    return "Close";
                else
                    return flowInstance.CurrentActiveStep.StepCode;
            }
        }

        private void Page_Load(object sender, System.EventArgs e)
        {
            // Put user code to initialize the page here
            if (!Page.IsPostBack)
            {
                this.btnReject.Attributes.Add("onclick", "javascript:OpenCommentPane();");
                this.btnReply.Attributes.Add("onclick", "javascript:OpenReplyPane(" + Request.QueryString["FlowID"] + ");");
                this.btnSubmit.Attributes.Add("onclick", "javascript:OpenAssignPane(" + Request.QueryString["AppID"] + ");");
                this.btnCheck.Attributes.Add("onclick", "javascript:OnCheck(" + Request.QueryString["AppID"] + ");");
                this.btnQuery.Attributes.Add("onclick", "javascript:OpenQueryPane(" + Request.QueryString["AppID"] + ");");
                this.btnClose.Attributes.Add("onclick", "javascript:SimpleCloseWindow()");


                this.uclOCV.DisableSSME();
            }
            ButtonStatusSetting();
            HideReportButton();
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        ///		Required method for Designer support - do not modify
        ///		the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.btnQuery.Click += new System.EventHandler(this.btnQuery_Click);
            this.btnReply.Click += new System.EventHandler(this.btnReply_Click);
            this.btnSubmit.Click += new System.EventHandler(this.btnSubmit_Click);
            this.btnCheck.Click += new System.EventHandler(this.btnCheck_Click);
            this.btnReject.Click += new System.EventHandler(this.btnReject_Click);
            this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
            this.Btn_Report.Click += new EventHandler(Btn_Report_Click);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion


        private void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                string temp = this.tbOperationContent.Text;
                string employeeid = temp.Substring(0, temp.IndexOf("_"));
                //string needEVP = temp.Substring(temp.IndexOf("_")+1);
                //if(needEVP.ToUpper() == "YES")
                //	this.uclOCV.NeedSSMEEVPApprove();
                this.uclOCV.Save();

                string msg = BEGDC.CC.BL.CCFlow.AttachmentService.GetMissedAttachmentMessage(this.AppID);
                if (msg + string.Empty != string.Empty)
                {
                    Page.RegisterClientScriptBlock("", "<script>alert('" + BEGDC.CC.Resource.SystemMessage.CM003 + "');</script>");
                    return;
                }

                IOCV ocvService = AppContainer.Instance[ServiceConst.OCVService] as IOCV;
                if (this.tbOperationFlag.Text == "assignment")
                {
                    ocvService.Assign(Convert.ToInt64(Request.QueryString["FlowID"]), OCVTransitionList.Assign, employeeid,
                        Convert.ToInt32(Request.QueryString["AppID"]), string.Empty);
                }
                Page.RegisterClientScriptBlock("", "<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CV001 + "');window.close();</script>");
            }
            catch (Exception exp)
            {
                if (exp.InnerException != null)
                    Page.RegisterClientScriptBlock("", "<script>alert('" + exp.InnerException.Message + "');</script>");
                else
                    Page.RegisterClientScriptBlock("", "<script>alert('" + BEGDC.CC.Resource.SystemMessage.CV002 + "');</script>");
            }
        }

        private void ControlStatusSetting(Accessibility access)
        {
            if (access == Accessibility.Readonly)
            {
                this.DisableChildrenControls(this);
            }
        }

        private void btnCheck_Click(object sender, EventArgs e)
        {
            try
            {
                //string temp = this.tbOperationContent.Text;
                //string employeeid = string.Empty;
                //string needEVP = string.Empty;
                //if(temp != string.Empty)
                //{
                //    employeeid = temp.Substring(0, temp.IndexOf("_"));
                //    needEVP = temp.Substring(temp.IndexOf("_")+1);
                //}
                //if(needEVP.ToUpper() == "YES")
                //    this.uclOCV.NeedSSMEEVPApprove();
                this.uclOCV.Save();

                string msg = BEGDC.CC.BL.CCFlow.AttachmentService.GetMissedAttachmentMessage(this.AppID);
                if (msg + string.Empty != string.Empty)
                {
                    Page.RegisterClientScriptBlock("", "<script>alert('" + BEGDC.CC.Resource.SystemMessage.CM003 + "')</script>");
                    return;
                }

                IOCV ocvService = AppContainer.Instance[ServiceConst.OCVService] as IOCV;
                ocvService.Check(Convert.ToInt64(Request.QueryString["FlowID"]), CVTransitionList.Check, WebUtility.DeCodeString(Server, tbComment.Text));
                Page.RegisterClientScriptBlock("", "<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CV001 + "');</script><script>window.close();</script>");
            }
            catch (Exception exp)
            {
                if (exp.InnerException != null)
                    Page.RegisterClientScriptBlock("", "<script>alert('" + exp.InnerException.Message + "');</script>");
                else
                    Page.RegisterClientScriptBlock("", "<script>alert('" + BEGDC.CC.Resource.SystemMessage.CV002 + "');</script>");
            }
        }

        private void btnReject_Click(object sender, EventArgs e)
        {
            try
            {
                IOCV ocvService = AppContainer.Instance[ServiceConst.OCVService] as IOCV;

                ocvService.Assign(Convert.ToInt64(Request.QueryString["FlowID"]), CVTransitionList.Reject, this.tbOperationContent.Text,
                    Convert.ToInt32(Request.QueryString["AppID"]), WebUtility.DeCodeString(Server, tbComment.Text));
                Page.RegisterClientScriptBlock("", "<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CV001 + "');</script><script>window.close();</script>");
            }
            catch (Exception exp)
            {
                if (exp.InnerException != null)
                    Page.RegisterClientScriptBlock("", "<script>alert('" + exp.InnerException.Message + "');</script>");
                else
                    Page.RegisterClientScriptBlock("", "<script>alert('" + BEGDC.CC.Resource.SystemMessage.CV002 + "');</script>");
            }
        }

        private void Btn_Export_Click(object sender, EventArgs e)
        {
            try
            {
                ExcelTool.ICCExcelExport excelExport = new ExcelTool.ExportCVForm();
                excelExport.Export(this.Page, this.AppID);
                Page.RegisterClientScriptBlock("", "<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM001 + "');</script>");
            }
            catch (BEGDC.Utility.ExceptionManagement.GDCException exp)
            {
                Page.RegisterClientScriptBlock("", "<script>alert('" + BEGDC.CC.Resource.ErrorMessage.Export_0001 + "');</script>");
            }

        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.tbOperationFlag.Text == "query")
                {
                    IOCV ocvService = AppContainer.Instance[ServiceConst.OCVService] as IOCV;
                    ocvService.Query(Convert.ToInt64(Request.QueryString["FlowID"]), this.tbOperationContent.Text, Session["Comment"].ToString());
                }
                Page.RegisterClientScriptBlock("", "<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CV001 + "');window.close();</script>");
            }
            catch
            {
                Page.RegisterClientScriptBlock("", "<script>alert('" + BEGDC.CC.Resource.SystemMessage.CV002 + "');</script>");
            }
        }

        protected override void ButtonStatusSetting()
        {
            if (this.Access == null)
                return;

            this.btnSubmit.Visible = this.Access.IsButtonVisible(OCVTransitionList.Assign);
            this.btnCheck.Visible = this.Access.IsButtonVisible(OCVTransitionList.Check);
            this.btnReject.Visible = this.Access.IsButtonVisible(OCVTransitionList.Reject);
            this.btnReply.Visible = this.Access.IsButtonVisible(OCVTransitionList.Reply) && this.Step == WorkTransition.OCV_Assignment;
            this.btnQuery.Visible = this.Access.IsButtonVisible(OCVTransitionList.Query);
            this.uclOCV.cvPrintCtrl.ProcessType = WorkflowTypes.OCV;
            this.uclOCV.cvPrintCtrl.Visible = this.Access.IsButtonVisible(OCVTransitionList.Copy);
        }

        private void btnReply_Click(object sender, EventArgs e)
        {
            try
            {
                this.uclOCV.Save();
                IOCV ocvService = AppContainer.Instance[ServiceConst.OCVService] as IOCV;
                if (this.tbOperationFlag.Text == "reply")
                {
                    this.uclOCV.Save();
                    ocvService.ReplyQuery(Convert.ToInt64(Request.QueryString["FlowID"]), WebUtility.DeCodeString(Server, tbComment.Text));
                }
                Page.RegisterClientScriptBlock("", "<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CV001 + "');window.close();</script>");
            }
            catch
            {
                Page.RegisterClientScriptBlock("", "<script>alert('" + BEGDC.CC.Resource.SystemMessage.CV002 + "');</script>");
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                this.uclOCV.Save();
                string url = string.Empty + Request.Form["tbURL"];
                string script = "<script>";
                if (url == string.Empty)
                    script += "VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM001 + "');";
                if (url != string.Empty)
                {
                    script += "window.location.href='" + url + "'";
                }
                script += "</script>";

                Page.RegisterClientScriptBlock("", script);
            }
            catch
            {
                Page.RegisterClientScriptBlock("", "<script>alert('" + BEGDC.CC.Resource.SystemMessage.CV002 + "');</script>");
            }
        }

        #region ReportTo
        void Btn_Report_Click(object sender, EventArgs e)
        {
            try
            {
                IOCV OCVService = AppContainer.Instance[ServiceConst.OCVService] as IOCV;
                OCVService.ReportToEmployeeApproved(this.Application);
                Page.RegisterClientScriptBlock("", "<script>alert('" + BEGDC.CC.Resource.SystemMessage.CV001 + "');window.close();</script>");
            }
            catch (Exception exp)
            {
                if (exp.InnerException != null)
                    Page.RegisterClientScriptBlock("", "<script>alert('" + exp.InnerException.Message + "');</script>");
                else
                    Page.RegisterClientScriptBlock("", "<script>alert('" + BEGDC.CC.Resource.SystemMessage.CV002 + "');</script>");
            }
        }

        private void HideReportButton()
        {
            BEGDC.CC.BL.Workflow.CCIdentity identity = (BEGDC.CC.BL.Workflow.CCIdentity)this.Page.User.Identity;
            IUser userService = AppContainer.Instance[ServiceConst.UserService] as IUser;
            if (userService.IsInRole(identity.UserInfo.NameEN, BEGDC.CC.BL.Workflow.UserRole.OCVMgr))
            {
                this.Btn_Report.Visible = false;
            }
            else
            {
                this.Btn_Report.Visible = true;
                this.btnReply.Visible = this.Access.IsButtonVisible(OCVTransitionList.Reply);
                this.btnSave.Visible = false;
                this.btnSubmit.Visible = false;
                this.btnCheck.Visible = false;
                if (this.btnReply.Visible) this.Btn_Report.Visible = false;
            }
        }
        #endregion

    }
}
